package com.cy.jxc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.jxc.MyException.ServiceExecption;
import com.cy.jxc.dao.ClientDao;
import com.cy.jxc.dao.ResposityDao;
import com.cy.jxc.entity.Client;
import com.cy.jxc.entity.Resposity;
import com.cy.jxc.service.ClientService;
import com.cy.jxc.service.GoodsService;
import com.cy.jxc.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClientServiceImpl  implements ClientService {
    @Autowired
    ClientDao clientDao;
    @Autowired
    ResposityDao resposityDao;
    @Override
    public List<Client> findClientObject(){

        List<Client> rows = clientDao.selectList(null);
        return rows;
    }
    @Override
    public JsonResult findClientByPage(int page, int rows) {
        /*if(page>clientDao.selectCount(null)/rows){
            throw new RuntimeException("当前页没有数据");
        }*/
        QueryWrapper<Client> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByAsc("clientid");
        //select * from client limit page,rows
        IPage<Client> clientIPage = clientDao.selectPage(new Page<Client>(page, rows), queryWrapper);
        long total = clientIPage.getTotal();
        System.out.println(total);
//        List<Client> records = clientIPage.getRecords();

        System.out.println(clientIPage);
        return new JsonResult(clientIPage);
    }

    @Override
    @Transactional
    public Integer insertClient(Client entity) {
        int rows =0;
        Boolean flag= findClientAddByname(entity.getClientpayname());
        if (flag==false){
            throw  new ServiceExecption("添加客户信息失败,仓库中没有对应的商品信息");
        }
        if(entity!=null&&entity.getClientname()!=null){
            rows= clientDao.insert(entity);
        }else {
            throw new ServiceExecption("请先填写客户表单信息");
        }
        return rows;
    }

    @Override
    public int updateClient(Client entity) {
        int rows =0;
        if(entity!=null&&entity.getClientname()!=null){
            Integer clientid = entity.getClientid();
            QueryWrapper<Client> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("clientid", clientid);
            rows = clientDao.update(entity, queryWrapper);
        }
        return rows;
    }

    @Override
    public Client findClientById(Integer clientid) {
        return clientDao.selectById(clientid);
    }

    @Override
    public List<Client> findClientByname(String clientname) {
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();


        queryWrapper.like("clientname", clientname);
        //select * from client limit page,rows
        List<Client> clients = clientDao.selectList(queryWrapper);
        if (clients.size()==0||clients==null){
            throw  new ServiceExecption("没有对应的客户数据");
        }
        return clients;
    }

    @Override
    public Integer deleteClientById(Integer clientid) {
        QueryWrapper<Client> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("clientid", clientid);
        return clientDao.delete(queryWrapper);
    }

    @Override
    public Boolean findClientAddByname(String clientname) {
        QueryWrapper<Resposity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("respname", clientname);
        Resposity resposity =resposityDao.selectOne(queryWrapper);
        System.out.println(resposity);
        if (resposity!=null){
            return true;
        }
        return  false;
    }


}
